//index.js
//获取应用实例
const app = getApp()
let canvas = require('../../common/js/canvas.js');
let Axios = require('../../common/js/ajax.js');
let Mtils = require('../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 2, //当前导航
		cur_id: 1,
		followTk: false,
		cur_pId: 0,
		cancelRepTk: false,
		agreeTK: false,
		tips: false,
		height: 866,
		top: 106,
		imgHeight: 0,
		hcImg: '',
		canvasTk: false, //合成图片
		canvas_id: '',
		curTk_id: '', //当前分享弹窗id
		refund: [],
		store_id: 0,
		isIpx: app.globalData.isIpx ? true : false
	},
	//事件处理函数
	onLoad: function(e) {
		var _this = this;
		this.setData({
			store_id: e.id
		})
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		Axios.post(app.globalData.baseUrl + 'App/Store/Order/refund_list', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id,
			type: this.data.cur_id
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					refund: data
				})
				this.data.refund.forEach(function(item) {
					item.toggle = false;
				})
			} else {
				this.showModal(res)
			}
		})
		if (app.globalData.nickName && app.globalData.avatarUrl) {
			this.setData({
				nickName: app.globalData.nickName,
				avatarUrl: app.globalData.avatarUrl
			})
		}
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '退款管理'
		})
	},
	preventTouchMove: function() {

	},
	bubbling: function() {

	},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	handleCloseTap: function() {
		this.setData({
			followTk: false,
			canvasTk: false, //合成图片
			height: 0,
			cancelRepTk: false,
			tips: false,
			agreeTK: false
		})
	},
	handleEvtap: function(e) { //menu
		var id = e.currentTarget.dataset.id;
		var _this = this
		this.setData({
			refund: [],
			cur_id: id,
		})
		Axios.post(app.globalData.baseUrl + 'App/Store/Order/refund_list', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
			store_id: this.data.store_id,
			type: this.data.cur_id
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					refund: data
				})
				this.data.refund.forEach(function(item) {
					item.toggle = false;
				})
			} else {
				this.showModal(res)
			}
		})

	},
	handleFollowTap: function(e) { //关注
		var _this = this;
		var index = e.currentTarget.dataset.id
		var follow = _this.data.refund[index].is_follow
		var id = _this.data.refund[index].store_id
		if (follow == 1) {
			_this.setData({
				followTk: true,
				cur_pId: index,
			})
		} else {
			Axios.post(app.globalData.baseUrl + 'App/Store/Store/store_follow', {
				store_id: id,
				uid: this.uid,
				sign: this.Md5,
				time: this.timesp
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					_this.setData({
						['refund[' + (index) + '].is_follow']: 1
					})
				}
			})
		}
	},
	handleCancelFoTap: function(e) { //取消关注
		var _this = this;
		var index = e.currentTarget.dataset.id;
		var id = _this.data.refund[index].store_id
		var follow = _this.data.refund[index].is_follow
		if (follow == 1) {
			Axios.post(app.globalData.baseUrl + 'App/Store/Store/store_follow', {
				store_id: id,
				uid: this.uid,
				sign: this.Md5,
				time: this.timesp
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					_this.setData({
						['refund[' + (index) + '].is_follow']: 0,
						followTk: false
					})
				}
			})
		}

	},
	handleCancelOrderRep: function(e) { //拒绝退款申请弹窗
		var id = e.currentTarget.dataset.id;
		this.setData({
			cancelRepTk: true,
			cur_pId: id
		})
	},
	handleRepCancel: function(e) { //拒绝退款申请
		this.setData({
			//tips: true
		})
	},
	handleAgreeTapTk: function(e) {
		var id = e.currentTarget.dataset.id;
		this.setData({
			agreeTK: true,
			cur_pId: id,
		})
	},
	handleAgreeTap: function(e) {
		var id = e.currentTarget.dataset.id;
		this.setData({
			tips: true
		})
	},
	handleShareTap: function(e) { //分享弹窗		
		var index = e.currentTarget.dataset.index;
		var id = this.data.refund[index].id;
		var nowToggle = this.data.refund[index].toggle;
		this.setData({
			curTk_id: id,
			['refund[' + (index) + '].toggle']: !nowToggle
		})
	},
	handleCanvasTap: function(e) { //合成图片				
		var index = e.currentTarget.dataset.index;
		var id = this.data.refund[index].id
		//console.log(e.detail.x)
		var HEIGHT = 670;
		var _this = this;
		var picinit = 0;
		var logoinit = 0;
		var textH = canvas.heightComputed(this.data.refund[index].share_content, 510, 28, 37)
		picinit = textH
		textH += canvas.heightComputed(this.data.refund[index].goods_title, 560, 28, 37)
		logoinit = textH
		var fontWith = canvas.widthComputed(_this.data.nickName, 48)
		this.setData({
			height: (textH + HEIGHT),
			imgHeight: (textH + HEIGHT),
		})
		wx.showLoading({
			title: '绘图中···',
		})
		var avatarUrl = '';
		var temp_picture = '';
		wx.downloadFile({
			url: app.globalData.baseUrl + _this.data.avatarUrl,
			success: function(res) {
				avatarUrl = res.tempFilePath
				if (_this.data.refund[index].goods_icon != '') {
					wx.downloadFile({
						url: app.globalData.baseUrl + _this.data.refund[index].goods_icon,
						success: function(res) {
							temp_picture = res.tempFilePath
							draws()
						},
						fail: function(fres) {
							wx.hideLoading();
						}
					})
				} else {
					draws()
					wx.hideLoading();
				}
			},
			fail: function(fres) {
				wx.hideLoading();
			}
		})

		function draws() {
			app.ctx.setFillStyle('white')
			app.ctx.fillRect(0, 0, 650, (textH + HEIGHT) * .552)
			var avatar = {
				src: avatarUrl,
				x: 32,
				y: 53,
				width: 88,
				height: 88
			}
			canvas.drawAvatar(avatar)
			var proImg = {
				src: temp_picture,
				x: 178,
				y: 118 + picinit,
				width: 285,
				height: 285
			}
			canvas.drawImg(proImg)
			var canvasLogo = {
				src: '/img/canvasLogo.png',
				x: 43,
				y: 498 + logoinit,
				width: 333,
				height: 39
			}
			canvas.drawImg(canvasLogo)
			var wxLogo = {
				src: '/img/xcx-ewm.jpg',
				x: 468,
				y: 455 + logoinit,
				width: 138,
				height: 166
			}
			canvas.drawImg(wxLogo)
			var author = {
				x: 139,
				y: 80,
				color: '#006e88',
				size: 28,
				align: 'left',
				baseline: 'middle',
				text: _this.data.nickName,
				bold: true
			}
			canvas.drawText(author)
			var sign = {
				x: 158 + fontWith,
				y: 80,
				color: '#f08415',
				size: 28,
				align: 'left',
				baseline: 'middle',
				text: '推荐',
				bold: true
			}
			canvas.drawText(sign)
			var adverter = {
				x: 139,
				y: 128,
				width: 510,
				height: 34,
				line: 2,
				color: '#3f4446',
				size: 28,
				align: 'left',
				baseline: 'middle',
				bold: false,
				text: this.data.refund[index].share_content
			}
			canvas.textWrap(adverter)
			var desc = {
				x: 40,
				y: 455 + picinit,
				width: 560,
				height: 34,
				line: 2,
				color: '#3f4446',
				size: 28,
				align: 'left',
				baseline: 'middle',
				bold: false,
				text: this.data.refund[index].goods_title
			}
			canvas.textWrap(desc)
			app.ctx.draw()
			setTimeout(function() {
				wx.canvasToTempFilePath({
					x: 0,
					y: 0,
					canvasId: 'firstCanvas',
					success: function(res) {
						wx.hideLoading();
						// 设置画布高度						
						_this.setData({
							hcImg: res.tempFilePath,
							canvasTk: true, //合成图片
							canvas_id: id,
							//tk: true,
							height: 0
						})
					}
				});
			}, 1000)
		}
	},
	handleSaveTap: function() { //保存到相册
		console.log('保存到相册')
		wx.getImageInfo({
			src: this.data.hcImg, //
			success: function(res) {
				wx.saveImageToPhotosAlbum({
					filePath: res.path,
					success: function(dres) {
						wx.showToast({
							title: '已保存到相册'
						})
					},
					fail(res) {
						console.log("保存图片：fail");
						console.log(res);

					}
				})
			}
		})
	},
	onShareAppMessage: function(e) {
		var index = e.currentTarget.dataset.index
		var id = this.data.refund[index].id
		let _this = this;
		this.setData({
			shareTk: false,
		})
		return {
			title: _this.data.refund[id].share_content, // 转发后 所显示的title
			path: '/pages/goodDetail/goodDetail?id=' + id, // 相对的路径
			success: function(res) { // 成功后要做的事情
				console.log(res.shareTickets[0])
				// console.log
				wx.getShareInfo({
					shareTicket: res.shareTickets[0],
					success: function(res) {

					},
					fail: function(res) {
						console.log(res)
					},
					complete: function(res) {
						console.log(res)
					}
				})
			},
			fail: function(res) {
				// 分享失败
				console.log(res)
			}
		}
	},
})
